package com.dev;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.icu.util.Calendar;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.fb.FileBrowserPage;
import android.widget.EditText;
public class ExtraBarForIndentEditor extends LinearLayout{
	LinearLayout linear1;
	LinearLayout linear2;
	LayoutParams linear2lp=new LayoutParams(-1,0);
	key tab,k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,ka;
	key ek1,ek2,ek3,ek4,ek5,ek6,ek7,ek8,ek9,eka;
	Drawable backgroundSaved;
	public BaseIndentEditor to;
	public boolean num=false;
    public boolean capsLock=false;
	public int heightNow;
    public int heightDefault;
	final MainActivity ma;
	final ColorDrawable pressed=new ColorDrawable(0x66666655);
	public ExtraBarForIndentEditor(final MainActivity ma){
		super(ma);
		this.ma=ma;
        setOrientation(1);
		tab=new TabKey();
		k0=new TagKey();
		k1=new CapsLockKey();
		k2=new key("<");
		k3=new key(">");
		k4=new key("{");
		k5=new key("}");
		k6=new key("\\");
		k7=new key("ˌ");
        k8=new key("·");
		k9=new key("※");
		ka=new key("°");
		k0.setOnLongClickListener(new OnLongClickListener(){
			@Override
			public boolean onLongClick(View v){
				Calendar ca=Calendar.getInstance();
				int week=ca.get(ca.DAY_OF_WEEK);
				if(week==1){
					week=7;
				}else{week=week-1;}
				String t="["+ca.get(ca.YEAR)+"/"+(ca.get(ca.MONTH)+1)+"/"+ca.get(ca.DAY_OF_MONTH)+"("+week+")/";
				t+=ca.get(ca.HOUR_OF_DAY)+":"+ca.get(ca.MINUTE)+"]";
				to.commitText(t);
				/*View tab=ma.gui.tfNow;
				if(tab instanceof JavaEditorTab){
					JavaEditorTab jtNow=(JavaEditorTab)ma.gui.tfNow;
					JavaIndentEditor ie=jtNow.jie;
					int a=ie.selectionStart;
					int b=ie.selectionEnd;
					ie.repl(a,b,t);
					if(a<=b){
						ie.selectionStart+=t.length();
						ie.selectionEnd=ie.selectionStart;
					}else{
						ie.selectionEnd+=t.length();
						ie.selectionStart=ie.selectionEnd;
					}
					ie.invalidate();
					jtNow.canSave=true;
					jtNow.setTextColor(0xffff0000);
					ma.gui.ivSave.setColorFilter(null);
					ma.gui.ivSave.invalidate();
					ma.gui.ivRefresh.setColorFilter(null);
					ma.gui.ivRefresh.invalidate();
				}*/
				return true;
			}
		});
		heightNow=(int)(30*ma.dp);
        heightDefault=heightNow;
		linear1=new LinearLayout(ma);
		linear1.setOrientation(HORIZONTAL);
		linear1.addView(tab);
		linear1.addView(k0);
		linear1.addView(k1);
		linear1.addView(k2);
		linear1.addView(k3);
		linear1.addView(k4);
		linear1.addView(k5);
		linear1.addView(k6);
		linear1.addView(k7);
        linear1.addView(k8);
		linear1.addView(k9);
		linear1.addView(ka);
		ekClick ekc=new ekClick();
		ek1=new key("℃");
		ek1.setOnClickListener(ekc);
		ek2=new key("≤");
		ek2.setOnClickListener(ekc);
		ek3=new key("≥");
		ek3.setOnClickListener(ekc);
		ek4=new key("↑");
		ek4.setOnClickListener(ekc);
		ek5=new key("↓");
		ek5.setOnClickListener(ekc);
		ek6=new key("ρ");
		ek6.setOnClickListener(ekc);
		ek7=new key("Δ");
		ek7.setOnClickListener(ekc);
		ek8=new key("≡");
		ek8.setOnClickListener(ekc);
		ek9=new key("√");
		ek9.setOnClickListener(ekc);
		eka=new key("■");
		eka.setOnClickListener(ekc);
		linear2=new LinearLayout(ma);
		linear2.setOrientation(HORIZONTAL);
		linear2.addView(ek1);
		linear2.addView(ek2);
		linear2.addView(ek3);
		linear2.addView(ek4);
		linear2.addView(ek5);
		linear2.addView(ek6);
		linear2.addView(ek7);
		linear2.addView(ek8);
		linear2.addView(ek9);
		linear2.addView(eka);
		addView(linear1,-1,heightNow);
		addView(linear2,linear2lp);
	}
    boolean isCapsLock(){
        return capsLock;
    }
    void setCapsLock(boolean b){
        capsLock=b;
        k1.invalidate();
    }
	class ekClick implements OnClickListener{
		@Override
		public void onClick(View p){
			key v=(key)p;
			linear2lp.height=0;
			updateViewLayout(linear2,linear2lp);
            if(to==null){
                return;
            }
            if(!to.canEdit){
                return;
            }
            to.commitText(v.s);
			View tab=ma.gui.tfNow;
			if(tab instanceof JavaEditorTab){
				JavaEditorTab jtNow=(JavaEditorTab)ma.gui.tfNow;
				JavaIndentEditor ie=jtNow.jep.jie;
				if(ie!=null&&ie.text!=null&&ie.canEdit){
					jtNow.canSave=true;
					jtNow.setTextColor(0xffff0000);
					ma.gui.ivSave.setColorFilter(null);
					ma.gui.ivSave.invalidate();
					ma.gui.ivRefresh.setColorFilter(null);
					ma.gui.ivRefresh.invalidate();
				}
			}
		}
	}
	class key extends TextView implements OnClickListener{
		String s;
		public key(String s){
			super(ma);
			this.s=s;
			setLayoutParams(new LinearLayout.LayoutParams(0,-1,1));
			setGravity(Gravity.CENTER);
			setText(s);
			setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
			setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
			setTextColor(0xff000000);
			setOnClickListener(this);
			lenlim=(int)(15*ma.dp);
		}
		Point touch_down=new Point();
		Point touch_downRaw=new Point();
		long touch_timeSaved;
		int fingerMoved;
		int lenlim;
		int selectionStartSaved;
		int selectionEndSaved;
		int linear2lp_heightSaved;
		public boolean onTouchEvent(MotionEvent event) {
			int ex=(int)event.getX();
			int ey=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touch_down.x=ex;
					touch_down.y=ey;
					touch_downRaw.x=(int)event.getRawX();
					touch_downRaw.y=(int)event.getRawY();
					backgroundSaved=getBackground();
					fingerMoved=0;
					setBackgroundDrawable(pressed);
					linear2lp_heightSaved=linear2lp.height;
					selectionStartSaved=to.selectionStart;
					selectionEndSaved=to.selectionEnd;
					break;
				case MotionEvent.ACTION_MOVE:
					if(fingerMoved==0){
                        int xMoved=(int)event.getX()-touch_down.x;
                        int yMoved=(int)event.getY()-touch_down.y;
                        int xMovedAbs=Math.abs(xMoved);
                        int yMovedAbs=Math.abs(yMoved);
                        if(xMovedAbs>5||yMovedAbs>5){
                            if(xMovedAbs>yMovedAbs){
                                fingerMoved=2;
                            }else{
                                fingerMoved=1;
                            }
						}
					}else if(fingerMoved==1){
						int xMoved=(int)event.getRawX()-touch_downRaw.x;
                        int yMoved=(int)event.getRawY()-touch_downRaw.y;
                        int linear2lp_heightWill=linear2lp_heightSaved-yMoved;
                        if(linear2lp_heightWill<0){
                            linear2lp.height=0;
                            ExtraBarForIndentEditor.this.updateViewLayout(linear2,linear2lp);
                        }else if(linear2lp_heightWill>heightNow){
                            //Toast.makeText(ma,"ver moved",0).show();
                            linear2lp_heightWill=heightNow;
                            linear2lp.height=linear2lp_heightWill;
                            ExtraBarForIndentEditor.this.updateViewLayout(linear2,linear2lp);
                        }else{
                            linear2lp.height=linear2lp_heightWill;
                            ExtraBarForIndentEditor.this.updateViewLayout(linear2,linear2lp);
                        }
					}else if(fingerMoved==2){
                        if(to!=null){
                            int xMoved=(int)event.getX()-touch_down.x;
                            int selectionEndWill=selectionEndSaved+xMoved/lenlim;
                            to.letSelection(to.selectionStart,selectionEndWill);
                            to.tcv.againWait=true;
                            to.showCursor=true;
                            to.invalidate();
                        }
                    }
					break;
				case MotionEvent.ACTION_UP:
					setBackgroundDrawable(null);
					break;
				case MotionEvent.ACTION_CANCEL:
					setBackgroundColor(0x00ffffff);
					if(ey>=0&&ey<getHeight()){
						if(ex>=getWidth()){
						}else if(ex<0){
						}
					}
					break;
			}
			super.onTouchEvent(event);
			return true;
		}
		@Override
		public void onClick(View p1) {
            
            if(to==null){
                return;
            }else if(to.canEdit){
                to.commitText(s);
                View tab=ma.gui.tfNow;
                //int inta=destination.selectionStart;
                //int intb=destination.selectionEnd;
                if(tab instanceof HtmlEditorTab){
                    HtmlEditorTab h=(HtmlEditorTab)ma.gui.tfNow;
                    h.canSave=true;
                    h.setTextColor(0xffff0000);
                    ma.gui.ivSave.setColorFilter(null);
                    ma.gui.ivSave.invalidate();
                    ma.gui.ivRefresh.setColorFilter(null);
                    ma.gui.ivRefresh.invalidate();
                }else if(tab instanceof FileBrowserTab){
                    FileBrowserTab fbt=(FileBrowserTab)tab;
                    FileBrowserPage fbp=fbt.fileBrowserPage;
                    if(fbp.hie!=null&&fbp.hie.getVisibility()==VISIBLE){
                        fbp.editorCanSave=true;
                        fbt.setTextColor(0xffff0000);
                        ma.gui.ivSave.setColorFilter(null);
                        ma.gui.ivSave.invalidate();
                        ma.gui.ivRefresh.setColorFilter(null);
                        ma.gui.ivRefresh.invalidate();
                    }
                }else if(tab instanceof JavaEditorTab){
                    JavaEditorTab jtNow=(JavaEditorTab)ma.gui.tfNow;
                    JavaIndentEditor ie=jtNow.jep.jie;
                    ie.invalidate();
                    jtNow.canSave=true;
                    jtNow.setTextColor(0xffff0000);
                    ma.gui.ivSave.setColorFilter(null);
                    ma.gui.ivSave.invalidate();
                    ma.gui.ivRefresh.setColorFilter(null);
                    ma.gui.ivRefresh.invalidate();
                }else if(tab instanceof CEditorTab){
                    CEditorTab cet=(CEditorTab)ma.gui.tfNow;
                    cet.canSave=true;
                    cet.setTextColor(0xffff0000);
                    ma.gui.ivSave.setColorFilter(null);
                    ma.gui.ivSave.invalidate();
                    ma.gui.ivRefresh.setColorFilter(null);
                    ma.gui.ivRefresh.invalidate();
                }
            }
		}
	}
	private final class TabKey extends key {
		public TabKey(){
			super("↹");
			s="\t";
		}
	}
    public final class CapsLockKey extends key implements OnLongClickListener{
        Paint p=new Paint();
        int cl,ct,cr,cb,thin,pillar;
        public CapsLockKey(){
            super("");
            p.setStrokeWidth(3);
			setOnClickListener(this);
			setOnLongClickListener(this);
        }
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            setPadding(0,getMeasuredHeight()/5,0,getMeasuredHeight()/5);
            int spaceHor=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
            int spaceVer=getMeasuredHeight()-getPaddingTop()-getPaddingBottom();
            int widthWill=spaceVer*3/4;
            if(widthWill>spaceHor){
                cl=getPaddingLeft();
                cr=getMeasuredWidth()-getPaddingRight();
                int ch=(cr-cl)*4/3;
                ct=spaceVer-ch;
                ct/=2;
                ct+=getPaddingTop();
                cb=ct+ch;
                thin=ch/4;
            }else{
                ct=getPaddingTop();
                cb=getMeasuredHeight()-getPaddingBottom();
                int cw=(cb-ct)*3/4;
                cl=spaceHor-cw;
                cl/=2;
                cl+=getPaddingLeft();
                cr=cl+cw;
                thin=cw/4;
            }
            pillar=(cb-ct)/2+ct;
        }
        @Override
        public void onClick(View v) {
			capsLock=!capsLock;
           	invalidate();
        }
		@Override
		public boolean onLongClick(View v){
			num=!num;
			invalidate();
			return true;
		}
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
			if(num){
				int vw=getWidth();
				int vh=getHeight();
				int cx=vw/2;
				int cy=vh/2;
				int radius=(cb-ct)*2/5;
				p.setStyle(Paint.Style.STROKE);
				p.setStrokeWidth(radius/8);
				canvas.drawCircle(cx,cy,radius,p);
				p.setTextSize(radius*1.5f);
				p.setTextAlign(Paint.Align.CENTER);
				p.setStyle(Paint.Style.FILL);
				int yy=cy+(int)(p.getTextSize()/2-p.descent());
				canvas.drawText("n",cx,yy,p);
			}else{
				Path path=new Path();
				if(capsLock){
					p.setStyle(Paint.Style.FILL);
				}else{
					p.setStyle(Paint.Style.STROKE);
				}
				path.moveTo(cl+thin,cb);
				path.lineTo(cl+thin,pillar);
				path.lineTo(cl,pillar);
				path.lineTo(cl+(cr-cl)/2,ct);
				path.lineTo(cr,pillar);
				path.lineTo(cr-thin,pillar);
				path.lineTo(cr-thin,cb);
				path.close();
				canvas.drawPath(path,p);
			}
        }
    }
	private final class CtrlKey extends key{
		public CtrlKey(){
			super("CTRL");
		}
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					break;
				case MotionEvent.ACTION_UP:
			}
			super.onTouchEvent(null);
			return true;
		}
		@Override
		public void onClick(View p1) {
			super.onClick(p1);
		}
	}
	private final class TagKey extends key{
		TagKey(){
			super("TAG");
		}
		@Override
		public void onClick(View p1) {
            if(to==null){
                
            }else if(to.canEdit){
                Calendar cal=Calendar.getInstance();
                String tag="["+cal.get(cal.HOUR_OF_DAY)+":"+cal.get(cal.MINUTE)+"]";
                to.commitText(tag);
                View tab=ma.gui.tfNow;
                if(tab instanceof JavaEditorTab){
                    JavaEditorTab jtNow=(JavaEditorTab)ma.gui.tfNow;
                    jtNow.canSave=true;
                    jtNow.setTextColor(0xffff0000);
                    ma.gui.ivSave.setColorFilter(null);
                    ma.gui.ivSave.invalidate();
                    ma.gui.ivRefresh.setColorFilter(null);
                    ma.gui.ivRefresh.invalidate();
                }
            }
		}
	}
}
